﻿using MyBasicLib.Data.ORMLiteNew;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using MyBasicLib.Data;
using MyBasicLib.Utilities;
using Moq;
using RUC.Core.Entities;

namespace MyBasicLib.Test
{


    /// <summary>
    ///This is a test class for DataContextTest and is intended
    ///to contain all DataContextTest Unit Tests
    ///</summary>
    [TestClass()]
    public class DataContextTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
            //Config.Instence.ObjectElementList.Clear();

            //Config.Instence.ObjectElementList.Add(new ObjectElement()
            //{
            //    ServiceType = typeof(IDbQueryProvider),
            //    Property = "Object",
            //    Singleton = true,
            //    Type = typeof(Moq.Mock<IDbQueryProvider>),
            //    ConstructorArgCollection = new List<AbstractValueProvider>(),
            //    PropertyCollection = new List<AbstractValueProvider>()
            //});

        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void DataContextTest1()
        { 
            DataContext target = new DataContext();
            Assert.IsNotNull(target.Provider);
            Assert.AreEqual(30, target.CommandTimeout); 
        }
       
        [TestMethod()]
        public void DataContextTest3()
        {
            var pro = new MyBasicLib.Data.ORMLiteNew.Translation.SqlClient.DbQueryProvider(new MyBasicLib.Data.SqlClient.Database("321"));
            DataContext target = new DataContext(pro);
            Assert.IsNotNull(target.Provider);
            Assert.AreEqual(30, target.CommandTimeout);
            Assert.AreEqual("321", (target.Provider as DbQueryProvider).ConnectionString);
        }
        //[TestMethod()]
        //public void DataContextTest4()
        //{
        //    var dc = new DataContext();
        //    Assert.IsNotNull(dc.Provider);
        //    Assert.AreEqual(30, dc.CommandTimeout);
        //    Assert.AreEqual("", (dc.Provider as DbQueryProvider).ConnectionString);
        //}


        [TestMethod()]
        public void GetTableTest()
        {
            DataContext target = new DataContext(); // TODO: Initialize to an appropriate value
            var mock = new Moq.Mock<IDbQueryProvider>();
            target.Provider = mock.Object;
            target.GetTable<App>().ToArray(); 
            mock.Verify(p => p.ExecuteList<App>(It.IsAny<IDbQueryable>()));

        }

        [TestMethod()]
        public void SubmitChangesTest()
        {
            DataContext target = new DataContext(); // TODO: Initialize to an appropriate value
            var mock = new Moq.Mock<IDbQueryProvider>();
            target.Provider = mock.Object;
            target.SubmitChanges(); 
            mock.Verify(p => p.SubmitChanges(It.Is<IList<IDbQueryable>>(i => i.Count == 0)));
        }
        [TestMethod()]
        public void SubmitChangesTest1()
        {
            DataContext target = new DataContext(); // TODO: Initialize to an appropriate value
            var mock = new Moq.Mock<IDbQueryProvider>();
            target.Provider = mock.Object;
            target.Insert(new App()); 
            target.SubmitChanges(); 
            mock.Verify(p => p.SubmitChanges(It.Is<IList<IDbQueryable>>(i => i.Count == 1)));
        }
    }
}
